clear
use usingfile 

global controls male age_arrival age_arrival_sq  highedu_arrival   missedu_arrival  married_arrival children_arrival unref   
global fe munic_arrival cntry immonth
global instrument munictiming  
global y avgemp  avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019


*******************************************
*TIME OF ARRIVAL AND PARTICIPATION
*******************************************

twoway line  mever_intro immonth_imyear2, xtitle("Month of arrival") ytitle("Share enrolled in Introductory program") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white)) plotregion(style(none)) color(black)  ms(O) xlabel(1 "Sep 02" 2 "Oct 02" 3 "Nov 02" 4 "Dec 02" 5 "Jan 03" 6 "Feb 03" 7 "Mar 03" 8 "Apr 03" 9 "May 03" 10 "Jun 03" 11 "Jul 03" 12 "Aug 03" 13 "Sep 03" 14 "Oct 03" 15 "Nov 03" 16 "Dec 03" 17 "Jan 04" 18 "Feb 04" 19 "Mar 04" 20 "Apr 04" 21 "May 04" 22 "Jun 04" 23 "Jul 04" 24 "Aug 04", alternate angle(270)) xline(13, lpat(shortdash_dot))
graph export intro_month.pdf, replace

twoway lowess  mever_intro immonth_imyear2, xtitle("Month of arrival") ytitle("Share enrolled in Introductory program") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white)) plotregion(style(none)) color(black)  ms(O) xlabel(1 "Sep 02" 2 "Oct 02" 3 "Nov 02" 4 "Dec 02" 5 "Jan 03" 6 "Feb 03" 7 "Mar 03" 8 "Apr 03" 9 "May 03" 10 "Jun 03" 11 "Jul 03" 12 "Aug 03" 13 "Sep 03" 14 "Oct 03" 15 "Nov 03" 16 "Dec 03" 17 "Jan 04" 18 "Feb 04" 19 "Mar 04" 20 "Apr 04" 21 "May 04" 22 "Jun 04" 23 "Jul 04" 24 "Aug 04", alternate angle(270)) xline(13, lpat(shortdash_dot))
graph export intro_month_lowess.pdf, replace

*******************************************
*DATE OF FIRST ENROLLED
*******************************************
distplot firstdate if tagfirstdate!=., ylabel(, angle(0) nogrid) legend(off) graphr(color(white)) plotregion(style(none)) xtitle("Date of first enrollment")   xlabel(#12, alternate angle(270))
graph export firstdate.pdf, replace

*******************************************
*DATE OF FIRST ARRIVAL ENROLLED
*******************************************
distplot first_forstdato if tagfirst_forstdato!=., ylabel(, angle(0) nogrid) legend(off) graphr(color(white)) plotregion(style(none)) xtitle("Arrival date of first enrolled")  xlabel(#12, alternate angle(270))
graph export firstarr.pdf, replace

*******************************************
*RD FIG
*******************************************

lowess ever_intro munictiming2  if munictiming2<0, ms(none) title("Lowess smoother early arrivals") ytitle("Enrolled") xtitle("Arrival")
graph save ever_pre.gph, replace
lowess ever_intro munictiming2  if munictiming2>=0, ms(none) title("Lowess smoother late arrivals") ytitle("Enrolled") xtitle("Arrival")
graph save ever_post.gph, replace
graph combine ever_pre.gph ever_post.gph, ycommon
graph export ever_pre_post.pdf, replace

*******************************************
*SHARE OF MUNICIPALITIES ENROLLING
*******************************************

twoway line  shsetmunic imq_imyr, xtitle("Quarter of arrival") ytitle("Share of municipalities enrolling refugees") ysc(r(0(.2)1)) ylabel(0(.2)1, angle(0) nogrid) legend(off)  graphr(color(white)) plotregion(style(none)) color(black)  ms(O) xlabel(1 "Sep 02-Nov 02" 2 "Dec 02-Feb 03" 3 "Mar 03-May 03" 4 "Jun 03-Aug 03" 5 "Sep 03-Nov 03" 6 "Dec 03-Feb 04" 7 "Mar 04-May 04" 8 "Jun 04-Aug 04", alternate angle(270)) xline(13, lpat(shortdash_dot))
graph export share_enrolling.pdf, replace

*******************************************
*EARNINGS TRAJECTORY
*******************************************
sort lopenr

foreach t of numlist 4/14{
    reghdfe cumwage`t' if ever_intro==1, noabsorb cl(munic_arrival)
	est store yr`t'_1
}
foreach t of numlist 4/14{
    reghdfe cumwage`t' if ever_intro==0, noabsorb cl(munic_arrival)
	est store yr`t'_0
}


coefplot  (yr4_1 yr5_1  yr6_1 yr7_1 yr8_1 yr9_1 yr10_1 yr11_1 yr12_1 yr13_1 yr14_1) (yr4_0 yr5_0 yr6_0 yr7_0 yr8_0 yr9_0 yr10_0 yr11_0 yr12_0 yr13_0 yr14_0, color(red)), keep(_cons) vertical aseq swapnames eqrename(yr4_0 = "4" yr5_0 = "5" yr6_0 = "6" yr7_0="7" yr8_0 = "8" yr9_0="9" yr10_0 = "10" yr11_0 = "11" yr12_0 = "12" yr13_0 = "13" yr14_0="14" yr4_1 = "4" yr5_1 = "5" yr6_1 = "6" yr7_1="7" yr8_1 = "8" yr9_1="9" yr10_1 = "10" yr11_1 = "11" yr12_1 = "12" yr13_1 = "13" yr14_1="14") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white)) plotregion(style(none)) color(black) ciopts(lpattern(dot)  lcolor(black))     xtitle("Years since arrival") ytitle("Cumulative earnings (in 10K NOK)")
graph export cumulative_wages_ols.pdf, replace

foreach t of numlist 4/14{
    reghdfe regwage`t' if ever_intro==1, noabsorb cl(munic_arrival)
	est store yr`t'_1
}
foreach t of numlist 4/14{
    reghdfe regwage`t' if ever_intro==0, noabsorb cl(munic_arrival)
	est store yr`t'_0
}

coefplot  (yr4_1 yr5_1  yr6_1 yr7_1 yr8_1 yr9_1 yr10_1 yr11_1 yr12_1 yr13_1 yr14_1) (yr4_0 yr5_0 yr6_0 yr7_0 yr8_0 yr9_0 yr10_0 yr11_0 yr12_0 yr13_0 yr14_0, color(red)),  keep(_cons) vertical aseq swapnames eqrename(yr4_0 = "4" yr5_0 = "5" yr6_0 = "6" yr7_0="7" yr8_0 = "8" yr9_0="9" yr10_0 = "10" yr11_0 = "11" yr12_0 = "12" yr13_0 = "13" yr14_0="14" yr4_1 = "4" yr5_1 = "5" yr6_1 = "6" yr7_1="7" yr8_1 = "8" yr9_1="9" yr10_1 = "10" yr11_1 = "11" yr12_1 = "12" yr13_1 = "13" yr14_1="14") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white)) plotregion(style(none)) color(black) ciopts(lpattern(dot) lcolor(black)) ms(O)   xtitle("Years since arrival") ytitle("Annual earnings (in 10K NOK)")
graph export annual_wages_ols.pdf, replace

*******************************************
*INSTRUMENT AND PARTICIPATION
*******************************************
lowess ever_intro munictiming, ms(none) title("") xtitle("Instrument") ytitle("Enrolled in introductory program") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white)) plotregion(style(none)) color(black)  
graph export intro_z.pdf, replace



*******************************************
*FIRST STAGE
*******************************************


reghdfe ever_intro $instrument, noabsorb cl(munic_arrival)
outreg2 using "firststage.tex",dec(3) label replace
test $instrument
reghdfe ever_intro $instrument, a($fe) cl(munic_arrival)
outreg2 using "firststage.tex",dec(3) label append
test $instrument
reghdfe ever_intro $instrument $controls, a($fe) cl(munic_arrival)
outreg2 using "firststage.tex",dec(3) label append
test $instrument


*******************************************
*INSTRUMENT AND COVARIATES
*******************************************
reghdfe $instrument $controls, a($fe) cl(munic_arrival)
outreg2 using "instrument_covar.tex",dec(3) label replace
test  $controls
test age_arrival age_arrival_sq
test highedu_arrival missedu_arrival
test married_arrival children_arrival

*******************************************
*INSTRUMENT AND OUTMIGRATION
*******************************************
reghdfe outmig $instrument, a($fe) cl(munic_arrival)
outreg2 using "instrument_outmig.tex",dec(3) label replace
reghdfe outmig $instrument $controls, a($fe) cl(munic_arrival)
outreg2 using "instrument_outmig.tex",dec(3) label append

*******************************************
*REDUCED FORM (endline)
*******************************************


foreach y in  $y{
    reghdfe `y' $instrument $controls, a($fe) cl(munic_arrival)
	est store `y'
	estadd local controls Yes
	estadd ysumm, replace
}

reghdfe intramarr14   $instrument  $controls   if married_arrival==0, a($fe) cl(munic_arrival)
	est store intramarr14
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace

 
#delimit;
esttab avgemp  avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 intramarr14 using "rf_endline.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls N ymean, fmt(0 0 2) labels ("Controls" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (munictiming) 
	varlabels(munictiming "Instrument" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Union }&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Voted  }&\multicolumn{1}{c}{  Voted}&\multicolumn{1}{c}{  Inter}\\
	&\multicolumn{1}{c}{Emp}&\multicolumn{1}{c}{Earnings}&\multicolumn{1}{c}{Soc ass}&\multicolumn{1}{c}{member}&\multicolumn{1}{c}{neighbors}&\multicolumn{1}{c}{citizen}&\multicolumn{1}{c}{  2017}&\multicolumn{1}{c}{  2019}&\multicolumn{1}{c}{  marriage}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr


*******************************************
*IV (endline)
*******************************************
foreach y in  $y{
    ivreghdfe `y' (ever_intro = $instrument) $controls, a($fe) cl(munic_arrival)
	est store `y'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}

#delimit;
esttab avgemp  avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 using "iv_endline.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Union }&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Voted  }&\multicolumn{1}{c}{  Voted}\\
	&\multicolumn{1}{c}{Emp}&\multicolumn{1}{c}{Earnings}&\multicolumn{1}{c}{Soc ass}&\multicolumn{1}{c}{member}&\multicolumn{1}{c}{neighbors}&\multicolumn{1}{c}{citizen}&\multicolumn{1}{c}{  2017}&\multicolumn{1}{c}{  2019}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

*UNEMP, DNA

merge m:1 knr_arrival imyear using fivavar
drop if _merge==2
drop _merge 
g socspend = Share_childcare +Share_education+ Share_elderlycare+ Share_healthsocial

foreach y in  $y{
    ivreghdfe `y' (ever_intro = $instrument) $controls   socspend , a($fe) cl(munic_arrival)
	est store `y'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}

#delimit;
esttab avgemp  avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 using "iv_endline_socspend.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro socspend) 
	varlabels(ever_intro "Enrollment" socspend "Social spending")
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Union }&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Voted  }&\multicolumn{1}{c}{  Voted}\\
	&\multicolumn{1}{c}{Emp}&\multicolumn{1}{c}{Earnings}&\multicolumn{1}{c}{Soc ass}&\multicolumn{1}{c}{member}&\multicolumn{1}{c}{neighbors}&\multicolumn{1}{c}{citizen}&\multicolumn{1}{c}{  2017}&\multicolumn{1}{c}{  2019}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

foreach y in  $y{
    ivreghdfe `y' (ever_intro = $instrument) $controls  unemployment, a($fe) cl(munic_arrival)
	est store `y'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}

#delimit;
esttab avgemp  avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 using "iv_endline_unemployment.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro unemployment) 
	varlabels(ever_intro "Enrollment" unemployment "Unemployment")
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Union }&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Voted  }&\multicolumn{1}{c}{  Voted}\\
	&\multicolumn{1}{c}{Emp}&\multicolumn{1}{c}{Earnings}&\multicolumn{1}{c}{Soc ass}&\multicolumn{1}{c}{member}&\multicolumn{1}{c}{neighbors}&\multicolumn{1}{c}{citizen}&\multicolumn{1}{c}{  2017}&\multicolumn{1}{c}{  2019}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

global y2 avgemp  avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 intramarr14_a

foreach y in  $y2{
    ivreghdfe `y' (ever_intro = $instrument) $controls  VoteShareDNA, a($fe) cl(munic_arrival)
	est store `y'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}

#delimit;
esttab avgemp  avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 intramarr14_a using "iv_endline_dna.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro VoteShareDNA) 
	varlabels(ever_intro "Enrollment" VoteShareDNA "Voteshare Labor")
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Union }&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Voted  }&\multicolumn{1}{c}{  Voted}&\multicolumn{1}{c}{  Inter}\\
	&\multicolumn{1}{c}{Emp}&\multicolumn{1}{c}{Earnings}&\multicolumn{1}{c}{Soc ass}&\multicolumn{1}{c}{member}&\multicolumn{1}{c}{neighbors}&\multicolumn{1}{c}{citizen}&\multicolumn{1}{c}{  2017}&\multicolumn{1}{c}{  2019}&\multicolumn{1}{c}{  marriage}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr


foreach y in  $y2{
    ivreghdfe `y' (ever_intro = $instrument) $controls unemployment VoteShareDNA, a($fe) cl(munic_arrival)
	est store `y'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}

#delimit;
esttab avgemp  avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019  intramarr14_a  using "iv_endline_dna_un.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro VoteShareDNA unemployment) 
	varlabels(ever_intro "Enrollment" VoteShareDNA "Vote share Labor" unemployment "Unemployment rate")
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Union }&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Voted  }&\multicolumn{1}{c}{  Voted}&\multicolumn{1}{c}{  Inter}\\
	&\multicolumn{1}{c}{Emp}&\multicolumn{1}{c}{Earnings}&\multicolumn{1}{c}{Soc ass}&\multicolumn{1}{c}{member}&\multicolumn{1}{c}{neighbors}&\multicolumn{1}{c}{citizen}&\multicolumn{1}{c}{  2017}&\multicolumn{1}{c}{  2019}&\multicolumn{1}{c}{  marriage}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr


*******************************************
*BINARY INSTRUMENT (endline)
*******************************************

foreach y in  $y{
    ivreghdfe `y' (ever_intro = municivd) $controls, a($fe) cl(munic_arrival)
	est store `y'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}

#delimit;
esttab avgemp  avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 using "iv_endline_dummy.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Union }&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Voted  }&\multicolumn{1}{c}{  Voted}\\
	&\multicolumn{1}{c}{Emp}&\multicolumn{1}{c}{Earnings}&\multicolumn{1}{c}{Soc ass}&\multicolumn{1}{c}{member}&\multicolumn{1}{c}{neighbors}&\multicolumn{1}{c}{citizen}&\multicolumn{1}{c}{  2017}&\multicolumn{1}{c}{  2019}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr


*******************************************
*OLS (endline)
*******************************************
foreach y in  $y{
    reghdfe `y' ever_intro $controls, a($fe) cl(munic_arrival)
	est store `y'
	estadd local controls Yes
	estadd ysumm, replace
}

#delimit;
esttab avgemp  avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 using "ols_endline.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls  N ymean, fmt(0 0  2) labels ("Controls"  "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Union }&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Voted  }&\multicolumn{1}{c}{  Voted}\\
	&\multicolumn{1}{c}{Emp}&\multicolumn{1}{c}{Earnings}&\multicolumn{1}{c}{Soc ass}&\multicolumn{1}{c}{member}&\multicolumn{1}{c}{neighbors}&\multicolumn{1}{c}{citizen}&\multicolumn{1}{c}{  2017}&\multicolumn{1}{c}{  2019}\\ \hline )
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr




foreach y in avgemp_std avgwage_std avgsos_std everunion_std norw_y14_std norcitizen_std vote2017_std vote2019_std intramarr14_a_std{
	ivreghdfe `y' (ever_intro = $instrument) $controls, a($fe) cl(munic_arrival)
}

reghdfe ever_intro  $instrument  $controls if avgwage_std!=., a($fe) cl(munic_arrival) 
predict pever_intro_w if e(sample)
reghdfe ever_intro  $instrument  $controls if everunion_std!=., a($fe) cl(munic_arrival) 
predict pever_intro_u if e(sample)
reghdfe ever_intro  $instrument  $controls if norw_y14_std!=., a($fe) cl(munic_arrival) 
predict pever_intro_n if e(sample)
reghdfe ever_intro  $instrument  $controls if norcitizen_std!=., a($fe) cl(munic_arrival) 
predict pever_intro_c if e(sample)
reghdfe ever_intro  $instrument  $controls if vote2017_std!=., a($fe) cl(munic_arrival) 
predict pever_intro_v1 if e(sample)
reghdfe ever_intro  $instrument  $controls if vote2019_std!=., a($fe) cl(munic_arrival) 
predict pever_intro_v2 if e(sample)
reghdfe ever_intro  $instrument  $controls if intramarr14_a_std!=., a($fe) cl(munic_arrival) 
predict pever_intro_i if e(sample)

reg  avgwage_std  pever_intro_w   $controls i.munic_arrival i.cntry i.immonth 
est store m1
reg  everunion_std  pever_intro_u   $controls i.munic_arrival i.cntry i.immonth 
est store m2
reg  norw_y14_std  pever_intro_n   $controls i.munic_arrival i.cntry i.immonth 
est store m3
reg  norcitizen_std  pever_intro_c   $controls i.munic_arrival i.cntry i.immonth 
est store m4
reg  vote2017_std  pever_intro_v1   $controls i.munic_arrival i.cntry i.immonth 
est store m5
reg  vote2019_std  pever_intro_v2   $controls i.munic_arrival i.cntry i.immonth 
est store m6
reg intramarr14_a_std  pever_intro_i   $controls i.munic_arrival i.cntry i.immonth 
est store m7

suest m1 m2 m3 m4 m5 m6 m7, cl(munic_arrival)
test ([m1_mean]_b[pever_intro_w] = [m2_mean]_b[pever_intro_u])
test ([m1_mean]_b[pever_intro_w] = [m3_mean]_b[pever_intro_n])
test ([m1_mean]_b[pever_intro_w] = [m4_mean]_b[pever_intro_c])
test ([m1_mean]_b[pever_intro_w] = [m5_mean]_b[pever_intro_v1])
test ([m1_mean]_b[pever_intro_w] = [m6_mean]_b[pever_intro_v2])
test ([m1_mean]_b[pever_intro_w] = [m7_mean]_b[pever_intro_i])

*******************************************
*IV (cumulative)
*******************************************

*EARNINGS
foreach t of numlist 4/14{
    ivreghdfe cumwage`t' (ever_intro = $instrument) $controls, a($fe) cl(munic_arrival)
	est store yr`t'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}


coefplot  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14, keep(ever_intro) vertical aseq swapnames eqrename(yr4 = "4" yr5 = "5" yr6 = "6" yr7="7" yr8 = "8" yr9="9" yr10 = "10" yr11 = "11" yr12 = "12" yr13 = "13" yr14="14") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white)) plotregion(style(none)) color(black) ciopts(lpattern(dot) lcolor(black)) ms(O) nooffsets xtitle("Years since arrival") ytitle("Cumulative earnings (in 10K NOK)")
graph export cumulative_wages.pdf, replace

#delimit;
esttab  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14 using "iv_cumwage.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}\\
	&\multicolumn{1}{c}{4}&\multicolumn{1}{c}{5}&\multicolumn{1}{c}{6}&\multicolumn{1}{c}{7}&\multicolumn{1}{c}{8}&\multicolumn{1}{c}{9}&\multicolumn{1}{c}{10}&\multicolumn{1}{c}{11}&\multicolumn{1}{c}{12}&\multicolumn{1}{c}{13}&\multicolumn{1}{c}{14}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

ivreghdfe cumwage14_std (ever_intro = $instrument) $controls, a($fe) cl(munic_arrival)

ivreghdfe cumwage14 (ever_intro = $instrument) $controls, a($fe) cl(munic_arrival)

foreach t of numlist 4/14{
	reghdfe cumwage`t' $controls, a($fe) cl(munic_arrival) res(yres`t')
	reghdfe ever_intro   $instrument  $controls if cumwage`t'!=., a($fe) cl(munic_arrival)  
	predict pintro`t' if e(sample), xb
	reghdfe cumwage`t' pintro`t' $controls, a($fe) cl(munic_arrival)  
	reg cumwage`t' pintro`t' $controls i.cntry i.immonth, a(munic_arrival) cl(munic_arrival)
	eststo yr`t'_1: margins, at(pintro`t'=1) post
	reg cumwage`t' pintro`t' $controls i.cntry i.immonth, a(munic_arrival) cl(munic_arrival)
	eststo yr`t'_0: margins, at(pintro`t'=0) post
}

coefplot  (yr4_1 yr5_1  yr6_1 yr7_1 yr8_1 yr9_1 yr10_1 yr11_1 yr12_1 yr13_1 yr14_1) (yr4_0 yr5_0 yr6_0 yr7_0 yr8_0 yr9_0 yr10_0 yr11_0 yr12_0 yr13_0 yr14_0, color(gray)),  keep(_cons) vertical aseq swapnames eqrename(yr4_0 = "4" yr5_0 = "5" yr6_0 = "6" yr7_0="7" yr8_0 = "8" yr9_0="9" yr10_0 = "10" yr11_0 = "11" yr12_0 = "12" yr13_0 = "13" yr14_0="14" yr4_1 = "4" yr5_1 = "5" yr6_1 = "6" yr7_1="7" yr8_1 = "8" yr9_1="9" yr10_1 = "10" yr11_1 = "11" yr12_1 = "12" yr13_1 = "13" yr14_1="14") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white)) plotregion(style(none)) color(black) ciopts(lpattern(dot) lcolor(black)) ms(O)   recast(connected) lstyle(dots)  xtitle("Years since arrival") ytitle("Cumulative earnings (in 10K NOK)")
graph save cum_wages_piv.gph, replace
graph export cum_wages_piv.pdf, replace


foreach t of numlist 4/14{
	reghdfe regwage`t' $controls, a($fe) cl(munic_arrival) res(yresa`t')
	reghdfe ever_intro   $instrument  $controls if regwage`t'!=., a($fe) cl(munic_arrival)  
	predict pintroa`t' if e(sample), xb
	reghdfe regwage`t' pintroa`t' $controls, a($fe) cl(munic_arrival)  
	reg regwage`t' pintroa`t' $controls i.cntry i.immonth, a(munic_arrival) cl(munic_arrival)
	eststo yr`t'_1: margins, at(pintroa`t'=1) post
	reg regwage`t' pintroa`t' $controls i.cntry i.immonth, a(munic_arrival) cl(munic_arrival)
	eststo yr`t'_0: margins, at(pintroa`t'=0) post
}

coefplot  (yr4_1 yr5_1  yr6_1 yr7_1 yr8_1 yr9_1 yr10_1 yr11_1 yr12_1 yr13_1 yr14_1) (yr4_0 yr5_0 yr6_0 yr7_0 yr8_0 yr9_0 yr10_0 yr11_0 yr12_0 yr13_0 yr14_0, color(gray)),  keep(_cons) vertical aseq swapnames eqrename(yr4_0 = "4" yr5_0 = "5" yr6_0 = "6" yr7_0="7" yr8_0 = "8" yr9_0="9" yr10_0 = "10" yr11_0 = "11" yr12_0 = "12" yr13_0 = "13" yr14_0="14" yr4_1 = "4" yr5_1 = "5" yr6_1 = "6" yr7_1="7" yr8_1 = "8" yr9_1="9" yr10_1 = "10" yr11_1 = "11" yr12_1 = "12" yr13_1 = "13" yr14_1="14") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white)) plotregion(style(none)) color(black) ciopts(lpattern(dot) lcolor(black)) ms(O) recast(connected) lstyle(dots)  xtitle("Years since arrival") ytitle("Annual earnings (in 10K NOK)")
graph save annual_wages_piv.gph, replace
graph export annual_wages_piv.pdf, replace

*SOCIAL ASSISTANCE
foreach t of numlist 4/14{
    ivreghdfe cumsos`t' (ever_intro = $instrument) $controls, a($fe) cl(munic_arrival)
	est store yr`t'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}


coefplot  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14, keep(ever_intro) vertical aseq swapnames eqrename(yr4 = "4" yr5 = "5" yr6 = "6" yr7="7" yr8 = "8" yr9="9" yr10 = "10" yr11 = "11" yr12 = "12" yr13 = "13" yr14="14") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white))  yline(0, lpat(shortdash_dot)) plotregion(style(none)) color(black) ciopts(lpattern(dot) lcolor(black)) ms(O) nooffsets xtitle("Years since arrival") ytitle("Cumulative social assistance (in 10K NOK)")
graph export cumulative_socass.pdf, replace

#delimit;
esttab  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14 using "iv_cumsos.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}\\
	&\multicolumn{1}{c}{4}&\multicolumn{1}{c}{5}&\multicolumn{1}{c}{6}&\multicolumn{1}{c}{7}&\multicolumn{1}{c}{8}&\multicolumn{1}{c}{9}&\multicolumn{1}{c}{10}&\multicolumn{1}{c}{11}&\multicolumn{1}{c}{12}&\multicolumn{1}{c}{13}&\multicolumn{1}{c}{14}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

foreach t of numlist 4/14{
	reghdfe ever_intro   $instrument  $controls if cumsos`t'!=., a($fe) cl(munic_arrival)  
	predict pintros`t' if e(sample), xb
	reghdfe cumsos`t' pintros`t' $controls, a($fe) cl(munic_arrival)  
	reg cumsos`t' pintros`t' $controls i.cntry i.immonth, a(munic_arrival) cl(munic_arrival)
	eststo yr`t'_1: margins, at(pintros`t'=1) post
	reg cumsos`t' pintros`t' $controls i.cntry i.immonth, a(munic_arrival) cl(munic_arrival)
	eststo yr`t'_0: margins, at(pintros`t'=0) post
}

coefplot  (yr4_1 yr5_1  yr6_1 yr7_1 yr8_1 yr9_1 yr10_1 yr11_1 yr12_1 yr13_1 yr14_1) (yr4_0 yr5_0 yr6_0 yr7_0 yr8_0 yr9_0 yr10_0 yr11_0 yr12_0 yr13_0 yr14_0, color(gray)),  keep(_cons) vertical aseq swapnames eqrename(yr4_0 = "4" yr5_0 = "5" yr6_0 = "6" yr7_0="7" yr8_0 = "8" yr9_0="9" yr10_0 = "10" yr11_0 = "11" yr12_0 = "12" yr13_0 = "13" yr14_0="14" yr4_1 = "4" yr5_1 = "5" yr6_1 = "6" yr7_1="7" yr8_1 = "8" yr9_1="9" yr10_1 = "10" yr11_1 = "11" yr12_1 = "12" yr13_1 = "13" yr14_1="14") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white)) plotregion(style(none)) color(black) ciopts(lpattern(dot) lcolor(black)) ms(O)   recast(connected) lstyle(dots)  xtitle("Years since arrival") ytitle("Cumulative social assistance (in 10K NOK)")
graph save cum_sosass_piv.gph, replace
graph export cum_sosass_piv.pdf, replace


*UNION MEMBERSHIP
foreach t of numlist 4/14{
    ivreghdfe eunion`t' (ever_intro = $instrument) $controls, a($fe) cl(munic_arrival)
	est store yr`t'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}


coefplot  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14, keep(ever_intro) vertical aseq swapnames eqrename(yr4 = "4" yr5 = "5" yr6 = "6" yr7="7" yr8 = "8" yr9="9" yr10 = "10" yr11 = "11" yr12 = "12" yr13 = "13" yr14="14") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white))  yline(0, lpat(shortdash_dot)) plotregion(style(none)) color(black) ciopts(lpattern(dot) lcolor(black)) ms(O) nooffsets xtitle("Years since arrival") ytitle("Cumulative union membership")
graph export cumulative_union.pdf, replace

#delimit;
esttab  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14 using "iv_cumunion.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}\\
	&\multicolumn{1}{c}{4}&\multicolumn{1}{c}{5}&\multicolumn{1}{c}{6}&\multicolumn{1}{c}{7}&\multicolumn{1}{c}{8}&\multicolumn{1}{c}{9}&\multicolumn{1}{c}{10}&\multicolumn{1}{c}{11}&\multicolumn{1}{c}{12}&\multicolumn{1}{c}{13}&\multicolumn{1}{c}{14}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr


foreach t of numlist 4/14{
	reghdfe ever_intro   $instrument  $controls if eunion`t'!=., a($fe) cl(munic_arrival)  
	predict pintrou`t' if e(sample), xb
	reghdfe eunion`t' pintrou`t' $controls, a($fe) cl(munic_arrival)  
	reg  eunion`t' pintrou`t' $controls i.cntry i.immonth, a(munic_arrival) cl(munic_arrival)
	eststo yr`t'_1: margins, at(pintrou`t'=1) post
	reg  eunion`t' pintrou`t' $controls i.cntry i.immonth, a(munic_arrival) cl(munic_arrival)
	eststo yr`t'_0: margins, at(pintrou`t'=0) post
}

coefplot  (yr4_1 yr5_1  yr6_1 yr7_1 yr8_1 yr9_1 yr10_1 yr11_1 yr12_1 yr13_1 yr14_1) (yr4_0 yr5_0 yr6_0 yr7_0 yr8_0 yr9_0 yr10_0 yr11_0 yr12_0 yr13_0 yr14_0, color(gray)),  keep(_cons) vertical aseq swapnames eqrename(yr4_0 = "4" yr5_0 = "5" yr6_0 = "6" yr7_0="7" yr8_0 = "8" yr9_0="9" yr10_0 = "10" yr11_0 = "11" yr12_0 = "12" yr13_0 = "13" yr14_0="14" yr4_1 = "4" yr5_1 = "5" yr6_1 = "6" yr7_1="7" yr8_1 = "8" yr9_1="9" yr10_1 = "10" yr11_1 = "11" yr12_1 = "12" yr13_1 = "13" yr14_1="14") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white)) plotregion(style(none)) color(black) ciopts(lpattern(dot) lcolor(black)) ms(O)   recast(connected) lstyle(dots)  xtitle("Years since arrival") ytitle("Cumulative union membership")
graph save cumulative_union_piv.gph, replace
graph export cumulative_union_piv.pdf, replace


*EMPLOYMENT

foreach t of numlist 4/14{
	*reghdfe cumemp`t' $controls, a($fe) cl(munic_arrival) res(yres`t')
	reghdfe ever_intro   $instrument  $controls if cumemp`t'!=., a($fe) cl(munic_arrival)  
	predict pintroe`t' if e(sample), xb
	reghdfe cumemp`t' pintroe`t' $controls, a($fe) cl(munic_arrival)  
	reg cumemp`t' pintroe`t' $controls i.cntry i.immonth, a(munic_arrival) cl(munic_arrival)
	eststo yr`t'_1: margins, at(pintroe`t'=1) post
	reg cumemp`t' pintroe`t' $controls i.cntry i.immonth, a(munic_arrival) cl(munic_arrival)
	eststo yr`t'_0: margins, at(pintroe`t'=0) post
}

coefplot  (yr4_1 yr5_1  yr6_1 yr7_1 yr8_1 yr9_1 yr10_1 yr11_1 yr12_1 yr13_1 yr14_1) (yr4_0 yr5_0 yr6_0 yr7_0 yr8_0 yr9_0 yr10_0 yr11_0 yr12_0 yr13_0 yr14_0, color(gray)),  keep(_cons) vertical aseq swapnames eqrename(yr4_0 = "4" yr5_0 = "5" yr6_0 = "6" yr7_0="7" yr8_0 = "8" yr9_0="9" yr10_0 = "10" yr11_0 = "11" yr12_0 = "12" yr13_0 = "13" yr14_0="14" yr4_1 = "4" yr5_1 = "5" yr6_1 = "6" yr7_1="7" yr8_1 = "8" yr9_1="9" yr10_1 = "10" yr11_1 = "11" yr12_1 = "12" yr13_1 = "13" yr14_1="14") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white)) plotregion(style(none)) color(black) ciopts(lpattern(dot) lcolor(black)) ms(O)   recast(connected) lstyle(dots)  xtitle("Years since arrival") ytitle("Cumulative employment")
graph save cum_emp_piv.gph, replace
graph export cum_emp_piv.pdf, replace



foreach t of numlist 4/14{
    ivreghdfe cumemp`t' (ever_intro = $instrument) $controls, a($fe) cl(munic_arrival)
	est store yr`t'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}


#delimit;
esttab  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14 using "iv_cumemp.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}\\
	&\multicolumn{1}{c}{4}&\multicolumn{1}{c}{5}&\multicolumn{1}{c}{6}&\multicolumn{1}{c}{7}&\multicolumn{1}{c}{8}&\multicolumn{1}{c}{9}&\multicolumn{1}{c}{10}&\multicolumn{1}{c}{11}&\multicolumn{1}{c}{12}&\multicolumn{1}{c}{13}&\multicolumn{1}{c}{14}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr


*******************************************
*IV (annual)
*******************************************
*EMPLOYMENT
foreach t of numlist 4/14{
    ivreghdfe regemp`t' (ever_intro = $instrument) $controls, a($fe) cl(munic_arrival)
	est store yr`t'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}


coefplot  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14, keep(ever_intro) vertical aseq swapnames eqrename(yr4 = "4" yr5 = "5" yr6 = "6" yr7="7" yr8 = "8" yr9="9" yr10 = "10" yr11 = "11" yr12 = "12" yr13 = "13" yr14="14") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white))  yline(0, lpat(shortdash_dot)) plotregion(style(none)) color(black) ciopts(lpattern(dot) lcolor(black)) ms(O) nooffsets xtitle("Years since arrival") ytitle("Employment")
graph export annual_employment.pdf, replace

#delimit;
esttab  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14 using "iv_emp_annual.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}\\
	&\multicolumn{1}{c}{4}&\multicolumn{1}{c}{5}&\multicolumn{1}{c}{6}&\multicolumn{1}{c}{7}&\multicolumn{1}{c}{8}&\multicolumn{1}{c}{9}&\multicolumn{1}{c}{10}&\multicolumn{1}{c}{11}&\multicolumn{1}{c}{12}&\multicolumn{1}{c}{13}&\multicolumn{1}{c}{14}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

*EARNINGS
foreach t of numlist 4/14{
    ivreghdfe regwage`t' (ever_intro = $instrument) $controls, a($fe) cl(munic_arrival)
	est store yr`t'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}


coefplot  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14, keep(ever_intro) vertical aseq swapnames eqrename(yr4 = "4" yr5 = "5" yr6 = "6" yr7="7" yr8 = "8" yr9="9" yr10 = "10" yr11 = "11" yr12 = "12" yr13 = "13" yr14="14") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white))  yline(0, lpat(shortdash_dot)) plotregion(style(none)) color(black) ciopts(lpattern(dot) lcolor(black)) ms(O) nooffsets xtitle("Years since arrival") ytitle("Earnings (in 10K NOK)")
graph export annual_earnings.pdf, replace

#delimit;
esttab  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14 using "iv_earnings_annual.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}\\
	&\multicolumn{1}{c}{4}&\multicolumn{1}{c}{5}&\multicolumn{1}{c}{6}&\multicolumn{1}{c}{7}&\multicolumn{1}{c}{8}&\multicolumn{1}{c}{9}&\multicolumn{1}{c}{10}&\multicolumn{1}{c}{11}&\multicolumn{1}{c}{12}&\multicolumn{1}{c}{13}&\multicolumn{1}{c}{14}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr


*SOCIAL ASSISTANCE
foreach t of numlist 4/14{
    ivreghdfe regsos`t' (ever_intro = $instrument) $controls, a($fe) cl(munic_arrival)
	est store yr`t'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}


coefplot  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14, keep(ever_intro) vertical aseq swapnames eqrename(yr4 = "4" yr5 = "5" yr6 = "6" yr7="7" yr8 = "8" yr9="9" yr10 = "10" yr11 = "11" yr12 = "12" yr13 = "13" yr14="14") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white)) yline(0, lpat(shortdash_dot))  plotregion(style(none)) color(black) ciopts(lpattern(dot) lcolor(black)) ms(O) nooffsets xtitle("Years since arrival") ytitle("Social assistance (in 10K NOK)")
graph export annual_sosass.pdf, replace

#delimit;
esttab  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14 using "iv_socass_annual.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}\\
	&\multicolumn{1}{c}{4}&\multicolumn{1}{c}{5}&\multicolumn{1}{c}{6}&\multicolumn{1}{c}{7}&\multicolumn{1}{c}{8}&\multicolumn{1}{c}{9}&\multicolumn{1}{c}{10}&\multicolumn{1}{c}{11}&\multicolumn{1}{c}{12}&\multicolumn{1}{c}{13}&\multicolumn{1}{c}{14}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr



*SHARE NORWEGIANS
foreach t of numlist 4/14{
    ivreghdfe norw_y`t' (ever_intro = $instrument) $controls, a($fe) cl(munic_arrival)
	est store yr`t'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}


coefplot  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14, keep(ever_intro) vertical aseq swapnames eqrename(yr4 = "4" yr5 = "5" yr6 = "6" yr7="7" yr8 = "8" yr9="9" yr10 = "10" yr11 = "11" yr12 = "12" yr13 = "13" yr14="14") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white))  yline(0, lpat(shortdash_dot)) plotregion(style(none)) color(black) ciopts(lpattern(dot) lcolor(black)) ms(O) nooffsets xtitle("Years since arrival") ytitle("Norwegian neighbours")
graph export annual_norwegians.pdf, replace

#delimit;
esttab  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14 using "iv_norwegians_annual.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}\\
	&\multicolumn{1}{c}{4}&\multicolumn{1}{c}{5}&\multicolumn{1}{c}{6}&\multicolumn{1}{c}{7}&\multicolumn{1}{c}{8}&\multicolumn{1}{c}{9}&\multicolumn{1}{c}{10}&\multicolumn{1}{c}{11}&\multicolumn{1}{c}{12}&\multicolumn{1}{c}{13}&\multicolumn{1}{c}{14}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

foreach t of numlist 4/14{
	reghdfe norw_y`t' $controls, a($fe) cl(munic_arrival) 
	reghdfe ever_intro   $instrument  $controls if norw_y`t'!=., a($fe) cl(munic_arrival)  
	predict pintron`t' if e(sample), xb
	reghdfe norw_y`t' pintron`t' $controls, a($fe) cl(munic_arrival)  
	reg  norw_y`t' pintron`t' $controls i.cntry i.immonth, a(munic_arrival) cl(munic_arrival)
	eststo yr`t'_1: margins, at(pintron`t'=1) post
	reg  norw_y`t' pintron`t' $controls i.cntry i.immonth, a(munic_arrival) cl(munic_arrival)
	eststo yr`t'_0: margins, at(pintron`t'=0) post
}

coefplot  (yr4_1 yr5_1  yr6_1 yr7_1 yr8_1 yr9_1 yr10_1 yr11_1 yr12_1 yr13_1 yr14_1) (yr4_0 yr5_0 yr6_0 yr7_0 yr8_0 yr9_0 yr10_0 yr11_0 yr12_0 yr13_0 yr14_0, color(gray)),  keep(_cons) vertical aseq swapnames eqrename(yr4_0 = "4" yr5_0 = "5" yr6_0 = "6" yr7_0="7" yr8_0 = "8" yr9_0="9" yr10_0 = "10" yr11_0 = "11" yr12_0 = "12" yr13_0 = "13" yr14_0="14" yr4_1 = "4" yr5_1 = "5" yr6_1 = "6" yr7_1="7" yr8_1 = "8" yr9_1="9" yr10_1 = "10" yr11_1 = "11" yr12_1 = "12" yr13_1 = "13" yr14_1="14") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white)) plotregion(style(none)) color(black) ciopts(lpattern(dot) lcolor(black)) ms(O)  recast(connected) lstyle(dots)  xtitle("Years since arrival") ytitle("Norwegian neighbours")
graph save  annual_norwegians_piv.gph, replace
graph export  annual_norwegians_piv.pdf, replace



ivreghdfe yrmoved (ever_intro = $instrument) $controls, a($fe) cl(munic_arrival)
 	est store yrmoved
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
	
#delimit;
esttab yrmoved using "iv_yrmoved.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Years in}\\
	&\multicolumn{1}{c}{first neighborhood}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

*SHARE NORWEGIANS RELATIVE TO SHARE AT ARRIVAL
foreach t of numlist 4/14{
	g norw_y`t'_r  =    norw_y`t' -norw_arrival  
    ivreghdfe norw_y`t'_r  (ever_intro = $instrument) $controls, a($fe) cl(munic_arrival)
	est store yr`t'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}


coefplot  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14, keep(ever_intro) vertical aseq swapnames eqrename(yr4 = "4" yr5 = "5" yr6 = "6" yr7="7" yr8 = "8" yr9="9" yr10 = "10" yr11 = "11" yr12 = "12" yr13 = "13" yr14="14") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white))  yline(0, lpat(shortdash_dot)) plotregion(style(none)) color(black) ciopts(lpattern(dot) lcolor(black)) ms(O) nooffsets xtitle("Years since arrival") ytitle("Norwegian neighbours")
graph export annual_norwegians_rel.pdf, replace

#delimit;
esttab  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14 using "iv_norwegians_annual_rel.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}\\
	&\multicolumn{1}{c}{4}&\multicolumn{1}{c}{5}&\multicolumn{1}{c}{6}&\multicolumn{1}{c}{7}&\multicolumn{1}{c}{8}&\multicolumn{1}{c}{9}&\multicolumn{1}{c}{10}&\multicolumn{1}{c}{11}&\multicolumn{1}{c}{12}&\multicolumn{1}{c}{13}&\multicolumn{1}{c}{14}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

*MOVING
foreach t of numlist 4/14{
    ivreghdfe moved_knr_yr`t'  (ever_intro = $instrument) $controls, a($fe) cl(munic_arrival)
	est store yr`t'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}


coefplot  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14, keep(ever_intro) vertical aseq swapnames eqrename(yr4 = "4" yr5 = "5" yr6 = "6" yr7="7" yr8 = "8" yr9="9" yr10 = "10" yr11 = "11" yr12 = "12" yr13 = "13" yr14="14") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white))  yline(0, lpat(shortdash_dot)) plotregion(style(none)) color(black) ciopts(lpattern(dot) lcolor(black)) ms(O) nooffsets xtitle("Years since arrival") ytitle("Moved from assigned municipality")
graph export moved.pdf, replace

#delimit;
esttab  yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14 using "iv_moved.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}&\multicolumn{1}{c}{Yr}\\
	&\multicolumn{1}{c}{4}&\multicolumn{1}{c}{5}&\multicolumn{1}{c}{6}&\multicolumn{1}{c}{7}&\multicolumn{1}{c}{8}&\multicolumn{1}{c}{9}&\multicolumn{1}{c}{10}&\multicolumn{1}{c}{11}&\multicolumn{1}{c}{12}&\multicolumn{1}{c}{13}&\multicolumn{1}{c}{14}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

foreach y in avgemp avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 intramarr14_a{
    reghdfe `y' moved_early  $controls , a($fe) cl(munic_arrival)
	est store `y'
	estadd local controls Yes
	estadd ysumm, replace
}

#delimit;
esttab avgemp avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 intramarr14_a using "moved_early_main.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls N ymean, fmt(0 0 2) labels ("Controls" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (moved_early) 
	varlabels(moved_early "Early mover" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Union }&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Voted  }&\multicolumn{1}{c}{  Voted}&\multicolumn{1}{c}{  Inter}\\
	&\multicolumn{1}{c}{Emp}&\multicolumn{1}{c}{Earnings}&\multicolumn{1}{c}{Soc ass}&\multicolumn{1}{c}{member}&\multicolumn{1}{c}{neighbors}&\multicolumn{1}{c}{citizen}&\multicolumn{1}{c}{  2017}&\multicolumn{1}{c}{  2019}&\multicolumn{1}{c}{  marriage}\\ \hline  )
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

foreach y in avgemp avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 intramarr14_a{
    reghdfe `y' moved_early  $controls if lawright==0, a($fe) cl(munic_arrival)
	est store `y'
	estadd local controls Yes
	estadd ysumm, replace
}

#delimit;
esttab avgemp avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 intramarr14_a using "moved_early_noright.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls N ymean, fmt(0 0 2) labels ("Controls" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (moved_early) 
	varlabels(moved_early "Early mover" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Union }&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Voted  }&\multicolumn{1}{c}{  Voted}&\multicolumn{1}{c}{  Inter}\\
	&\multicolumn{1}{c}{Emp}&\multicolumn{1}{c}{Earnings}&\multicolumn{1}{c}{Soc ass}&\multicolumn{1}{c}{member}&\multicolumn{1}{c}{neighbors}&\multicolumn{1}{c}{citizen}&\multicolumn{1}{c}{  2017}&\multicolumn{1}{c}{  2019}&\multicolumn{1}{c}{  marriage}\\ \hline )
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

foreach y in avgemp avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 intramarr14_a{
    reghdfe `y' norw_arrival  $controls , a($fe) cl(munic_arrival)
	est store `y'
	estadd local controls Yes
	estadd ysumm, replace
}

#delimit;
esttab avgemp avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 intramarr14_a using "sh_norw_main.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls N ymean, fmt(0 0 2) labels ("Controls" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (norw_arrival) 
	varlabels(norw_arrival "Sh. Norw. arrival" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Union }&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Voted  }&\multicolumn{1}{c}{  Voted}&\multicolumn{1}{c}{  Inter}\\
	&\multicolumn{1}{c}{Emp}&\multicolumn{1}{c}{Earnings}&\multicolumn{1}{c}{Soc ass}&\multicolumn{1}{c}{member}&\multicolumn{1}{c}{neighbors}&\multicolumn{1}{c}{citizen}&\multicolumn{1}{c}{  2017}&\multicolumn{1}{c}{  2019}&\multicolumn{1}{c}{  marriage}\\ \hline  )
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr



foreach y in avgemp avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 intramarr14_a{
    reghdfe `y' norw_arrival  $controls if lawright==0, a($fe) cl(munic_arrival)
	est store `y'
	estadd local controls Yes
	estadd ysumm, replace
}

#delimit;
esttab avgemp avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 intramarr14_a using "sh_norw_main_noright.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls N ymean, fmt(0 0 2) labels ("Controls" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (norw_arrival) 
	varlabels(norw_arrival "Sh. Norw. arrival" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Union }&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Voted  }&\multicolumn{1}{c}{  Voted}&\multicolumn{1}{c}{  Inter}\\
	&\multicolumn{1}{c}{Emp}&\multicolumn{1}{c}{Earnings}&\multicolumn{1}{c}{Soc ass}&\multicolumn{1}{c}{member}&\multicolumn{1}{c}{neighbors}&\multicolumn{1}{c}{citizen}&\multicolumn{1}{c}{  2017}&\multicolumn{1}{c}{  2019}&\multicolumn{1}{c}{  marriage}\\ \hline  )
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

*share norwegians years 1-14
foreach t of numlist 1/14{
    ivreghdfe norw_y`t' (ever_intro = $instrument) $controls, a($fe) cl(munic_arrival)
	est store yr`t'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}


coefplot yr1 yr2 yr3 yr4 yr5  yr6 yr7 yr8 yr9 yr10 yr11 yr12 yr13 yr14, keep(ever_intro) vertical aseq swapnames eqrename(yr1 = "1" yr2 = "2" yr3 = "3" yr4 = "4" yr5 = "5" yr6 = "6" yr7="7" yr8 = "8" yr9="9" yr10 = "10" yr11 = "11" yr12 = "12" yr13 = "13" yr14="14") ylabel(, angle(0) nogrid) legend(off)  graphr(color(white))  yline(0, lpat(shortdash_dot)) plotregion(style(none)) color(black) ciopts(lpattern(dot) lcolor(black)) ms(O) nooffsets xtitle("Years since arrival") ytitle("Norwegian neighbours")
graph export annual_norwegians_full.pdf, replace


*SHARE FOREIGN BORN IN FIRM
ivreghdfe imshare_firm (ever_intro = $instrument) $controls, a($fe) cl(munic_arrival)
 	est store imshare_firm
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
	
#delimit;
esttab imshare_firm using "iv_imshare_firm.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Share of foreign-}\\
	&\multicolumn{1}{c}{born in firm}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

ivreghdfe imshare_firm  (ever_intro = $instrument) $controls if n_emp_firm>1, a($fe) cl(munic_arrival)
 	est store imshare_firm2
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace


*SHARE CO-ETHNICS IN FIRM

ivreghdfe coethshare_firm (ever_intro = $instrument) $controls , a($fe) cl(munic_arrival)
 	est store coethshare_firm
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace

ivreghdfe coethshare_firm (ever_intro = $instrument) $controls if n_emp_firm>1, a($fe) cl(munic_arrival)
 	est store coethshare_firm2
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace

#delimit;
esttab imshare_firm imshare_firm2  coethshare_firm  coethshare_firm2 using "iv_imshare_firm2.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Share of foreign-}&\multicolumn{1}{c}{Share of foreign-}&\multicolumn{1}{c}{Share of co-}&\multicolumn{1}{c}{Share of co-}\\
	&\multicolumn{1}{c}{born in firm}&\multicolumn{1}{c}{born in firm}&\multicolumn{1}{c}{nationals in firm}&\multicolumn{1}{c}{nationals in firm}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr
	
*******************************************
*IV (endline) MEN
*******************************************
foreach y in  $y{
    ivreghdfe `y' (ever_intro = $instrument) $controls if male==1, a($fe) cl(munic_arrival)
	est store `y'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}

#delimit;
esttab avgemp  avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 using "iv_endline_men.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Union }&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Voted  }&\multicolumn{1}{c}{  Voted}\\
	&\multicolumn{1}{c}{Emp}&\multicolumn{1}{c}{Earnings}&\multicolumn{1}{c}{Soc ass}&\multicolumn{1}{c}{member}&\multicolumn{1}{c}{neighbors}&\multicolumn{1}{c}{citizen}&\multicolumn{1}{c}{  2017}&\multicolumn{1}{c}{  2019}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

 *******************************************
*IV (endline) MUSLIM
*******************************************

global y avgemp  avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 intramarr14_a

foreach y in  $y{
    ivreghdfe `y' (ever_intro = $instrument) $controls if religion==4, a($fe) cl(munic_arrival)
	est store `y'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}

#delimit;
esttab avgemp  avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 intramarr14_a using "iv_endline_muslim.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Union }&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Voted  }&\multicolumn{1}{c}{  Voted}&\multicolumn{1}{c}{  Inter}\\
	&\multicolumn{1}{c}{Emp}&\multicolumn{1}{c}{Earnings}&\multicolumn{1}{c}{Soc ass}&\multicolumn{1}{c}{member}&\multicolumn{1}{c}{neighbors}&\multicolumn{1}{c}{citizen}&\multicolumn{1}{c}{  2017}&\multicolumn{1}{c}{  2019}&\multicolumn{1}{c}{  marriage}\\ \hline  )
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

*******************************************
*MARRIAGE IV (endline)
*******************************************
 
    ivreghdfe intramarr14 (ever_intro = $instrument) $controls if married_arrival==0, a($fe) cl(munic_arrival)
	est store intramarr14
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
 
#delimit;
esttab intramarr14 using "iv_endline_mar.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Married outside}\\
	&\multicolumn{1}{c}{own ancestry country}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

    ivreghdfe intramarr14 (ever_intro = $instrument) $controls if married_arrival==0 & male==1, a($fe) cl(munic_arrival)
 
*********************** 
*ENTROPY BALANCING
*********************** 
global instrument munictiming
ebct $controls, treatvar($instrument) 
rename _weight we
ebct $controls if married_arrival==0, treatvar($instrument) 
rename _weight wenomar

foreach y in  $y{
    ivreghdfe `y' (ever_intro = $instrument) $controls [aweight=we], a($fe) cl(munic_arrival)
	est store `y'
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
}

ivreghdfe intramarr14 (ever_intro = $instrument) $controls if married_arrival==0  [aweight=wenomar], a($fe) cl(munic_arrival)
	est store intramarr14
	estadd scalar ftest = e(widstat)
	estadd local controls Yes
	estadd ysumm, replace
 

#delimit;
esttab avgemp  avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 intramarr14 using "iv_endline_ebal.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls ftest N ymean, fmt(0 0 0 2) labels ("Controls" "F-stat" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro) 
	varlabels(ever_intro "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Union }&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Voted  }&\multicolumn{1}{c}{  Voted}&\multicolumn{1}{c}{  Inter}\\
	&\multicolumn{1}{c}{Emp}&\multicolumn{1}{c}{Earnings}&\multicolumn{1}{c}{Soc ass}&\multicolumn{1}{c}{member}&\multicolumn{1}{c}{neighbors}&\multicolumn{1}{c}{citizen}&\multicolumn{1}{c}{  2017}&\multicolumn{1}{c}{  2019}&\multicolumn{1}{c}{  marriage}\\ \hline  \emph{Second stage}\\)
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

global y avgemp  avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 intramarr14_a

foreach y in $y{
	ebalance ever_intro $controls if `y'!=.
	rename _webal w`y'
}


foreach y in  $y{
    reghdfe `y'  ever_intro   $controls [aweight=w`y'], a($fe) cl(munic_arrival)
	est store `y'
	estadd local controls Yes
	estadd ysumm, replace
}

#delimit;
esttab avgemp  avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019 intramarr14_a using "ols_ebalance.tex",
b(%12.3f) style(tex) starlevels(* 0.10 ** 0.05 *** 0.01)
	stats(controls N ymean, fmt(0 0 2) labels ("Controls" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
    keep (ever_intro ) 
	varlabels(ever_intro  "Enrollment" )
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	posthead(
		&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Avg}&\multicolumn{1}{c}{Union }&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Norw.}&\multicolumn{1}{c}{Voted  }&\multicolumn{1}{c}{  Voted}&\multicolumn{1}{c}{  Inter}\\
	&\multicolumn{1}{c}{Emp}&\multicolumn{1}{c}{Earnings}&\multicolumn{1}{c}{Soc ass}&\multicolumn{1}{c}{member}&\multicolumn{1}{c}{neighbors}&\multicolumn{1}{c}{citizen}&\multicolumn{1}{c}{  2017}&\multicolumn{1}{c}{  2019}&\multicolumn{1}{c}{  marriage}\\ \hline  )
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr



*******************************************
*DESCRIPTIVES
*******************************************
su  munictiming ever_intro male age_arrival age_arrival_sq  highedu_arrival   missedu_arrival  married_arrival children_arrival unref avgemp  avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019
 
bys ever_intro: su  munictiming ever_intro male age_arrival age_arrival_sq  highedu_arrival   missedu_arrival  married_arrival children_arrival unref avgemp  avgwage avgsos everunion norw_y14 norcitizen vote2017 vote2019

*********************** 
*ENROLLMENT PATTERNS
*********************** 
merge m:1 knr_arrival using fiva_pop_countyadm
drop if _merge==2
recode pop (0/9200=0)(9300/200000=1),g(largemunic)

bys immonth_imyear2: egen mpop = mean(pop)
twoway line  mpop immonth_imyear2, xtitle("Month of arrival") ytitle("Mean population in assignment municipality") ysc(r(0(5000)30000))ylabel(0(5000)30000, angle(0) nogrid) legend(off)  graphr(color(white)) plotregion(style(none)) color(black)  ms(O) xlabel(1 "Sep 02" 2 "Oct 02" 3 "Nov 02" 4 "Dec 02" 5 "Jan 03" 6 "Feb 03" 7 "Mar 03" 8 "Apr 03" 9 "May 03" 10 "Jun 03" 11 "Jul 03" 12 "Aug 03" 13 "Sep 03" 14 "Oct 03" 15 "Nov 03" 16 "Dec 03" 17 "Jan 04" 18 "Feb 04" 19 "Mar 04" 20 "Apr 04" 21 "May 04" 22 "Jun 04" 23 "Jul 04" 24 "Aug 04", alternate angle(270)) 

bys immonth_imyear2: egen mcountyadm = mean(CountyAdm)
twoway line  mcountyadm immonth_imyear2, xtitle("Month of arrival") ytitle("Mean population in assignment municipality") ysc(r(0(.1).5))ylabel(0(.1).5, angle(0) nogrid) legend(off)  graphr(color(white)) plotregion(style(none)) color(black)  ms(O) xlabel(1 "Sep 02" 2 "Oct 02" 3 "Nov 02" 4 "Dec 02" 5 "Jan 03" 6 "Feb 03" 7 "Mar 03" 8 "Apr 03" 9 "May 03" 10 "Jun 03" 11 "Jul 03" 12 "Aug 03" 13 "Sep 03" 14 "Oct 03" 15 "Nov 03" 16 "Dec 03" 17 "Jan 04" 18 "Feb 04" 19 "Mar 04" 20 "Apr 04" 21 "May 04" 22 "Jun 04" 23 "Jul 04" 24 "Aug 04", alternate angle(270)) 

bys immonth_imyear2: egen mlargemunic = mean(largemunic)
twoway line  mlargemunic immonth_imyear2, xtitle("Month of arrival") ytitle("Share assigned to large municipality") ysc(r(0(.2)1))ylabel(0(.2)1, angle(0) nogrid) legend(off)  graphr(color(white)) plotregion(style(none)) color(black)  ms(O) xlabel(1 "Sep 02" 2 "Oct 02" 3 "Nov 02" 4 "Dec 02" 5 "Jan 03" 6 "Feb 03" 7 "Mar 03" 8 "Apr 03" 9 "May 03" 10 "Jun 03" 11 "Jul 03" 12 "Aug 03" 13 "Sep 03" 14 "Oct 03" 15 "Nov 03" 16 "Dec 03" 17 "Jan 04" 18 "Feb 04" 19 "Mar 04" 20 "Apr 04" 21 "May 04" 22 "Jun 04" 23 "Jul 04" 24 "Aug 04", alternate angle(270)) 
graph export largemunic.pdf, replace

drop _merge
merge m:1 cntrybirth using cntryvar
drop if _merge==2
drop _merge
g lngdppc = ln( gle_rgdpc)
bys immonth_imyear2: egen mgdppc = mean( gle_rgdpc)
twoway line  mgdppc immonth_imyear2, xtitle("Month of arrival") ytitle("Mean GDP pc of arrivals")  ysc(r(0(1000)5000)) ylabel(0(1000)5000, angle(0) nogrid) legend(off)  graphr(color(white)) plotregion(style(none)) color(black)  ms(O) xlabel(1 "Sep 02" 2 "Oct 02" 3 "Nov 02" 4 "Dec 02" 5 "Jan 03" 6 "Feb 03" 7 "Mar 03" 8 "Apr 03" 9 "May 03" 10 "Jun 03" 11 "Jul 03" 12 "Aug 03" 13 "Sep 03" 14 "Oct 03" 15 "Nov 03" 16 "Dec 03" 17 "Jan 04" 18 "Feb 04" 19 "Mar 04" 20 "Apr 04" 21 "May 04" 22 "Jun 04" 23 "Jul 04" 24 "Aug 04", alternate angle(270)) 
graph export gdppc.pdf, replace


******************************************** 
*ADD NATIVE REFERENCE LINE & MODIFY FIGURES*
********************************************

clear

import delimited "estannual_native.gph.csv"

keep v___00001e v___00001f v___00001a v___00001b v___000016 v___000017 v___000012 v___000013 v___00000y v___00000z v___00000u v___00000v v___00000q v___00000r v___00000m v___00000n v___00000i v___00000j v___00000e v___00000f v___00000a v___00000b

g id = _n

drop v___00001e v___00001a v___000016 v___000012 v___00000y v___00000u v___00000q v___00000m v___00000i v___00000e v___00000a

egen b = rowfirst( v___00001f v___00001b v___000017 v___000013 v___00000z v___00000v v___00000r v___00000n v___00000j v___00000f v___00000b)
keep id b
g yr = (id-12)*(-1)

graph use  "annual_wages_piv.gph"

addplot:  connected b yr, m(O) ysc(r(5(10)40)) ylabel(10 20 30 40) color(gs14) xlabel(0 " " 1 "4" 2 "5" 3 "6" 4 "7" 5 "8" 6 "9" 7 "10" 8 "11" 9 "12" 10 "13" 11 "14") 


graph save annual_wages_with_natives.gph, replace
graph export annual_wages_with_natives.pdf, replace


clear

import delimited "estcumulative_native.gph.csv"

keep if set>21

keep v___00003v v___00003r v___00003n v___00003j v___00003f v___00003b v___000037 v___000033 v___00002z v___00002v v___00002r
g yr = _n
replace yr = (yr-12)*(-1)
egen b = rowfirst(v___00003v v___00003r v___00003n v___00003j v___00003f v___00003b v___000037 v___000033 v___00002z v___00002v v___00002r)
keep yr b

graph use  "cum_wages_piv"
addplot: connected b yr, ms(O) ysc(r(0(50)350)) ylabel(50 100 150 200 250  300 350) color(gs14) xlabel(0 " " 1 "4" 2 "5" 3 "6" 4 "7" 5 "8" 6 "9" 7 "10" 8 "11" 9 "12" 10 "13" 11 "14")  sort  

graph save cumulative_wages_with_natives.gph, replace
graph export cumulative_wages_with_natives.pdf, replace


clear

import delimited "estunion_native.gph.csv"

keep if set>32

keep v___000053 v___00004z v___00004v v___00004r v___00004n v___00004j v___00004f v___00004b v___000047 v___000043 v___00003z
g yr = _n
replace yr = (yr-12)*(-1)
egen b = rowfirst( v___000053 v___00004z v___00004v v___00004r v___00004n v___00004j v___00004f v___00004b v___000047 v___000043 v___00003z)
keep yr b


graph use "cumulative_union_piv.gph"
addplot: connected b yr, ms(O) ysc(r(0(.2).6)) ylabel(0 .2 .4  .6) color(gs14) xlabel(0 " " 1 "4" 2 "5" 3 "6" 4 "7" 5 "8" 6 "9" 7 "10" 8 "11" 9 "12" 10 "13" 11 "14") 


graph save cumulative_union_with_natives.gph, replace
graph export cumulative_union_with_natives.pdf, replace


clear

import delimited "estnorw_native.gph.csv"

keep if set>10

keep v___00002n v___00002j v___00002f v___00002b v___000027 v___000023 v___00001z v___00001v v___00001r v___00001n v___00001j

g yr = _n
replace yr = (yr-12)*(-1)
egen b = rowfirst(v___00002n v___00002j v___00002f v___00002b v___000027 v___000023 v___00001z v___00001v v___00001r v___00001n v___00001j)
keep yr b

graph use "annual_norwegians_piv"

addplot: connected b yr, ms(O) ysc(r(.75(.05).95)) ylabel(.75 .80 .85 .90 .95) color(gs14) xlabel(0 " " 1 "4" 2 "5" 3 "6" 4 "7" 5 "8" 6 "9" 7 "10" 8 "11" 9 "12" 10 "13" 11 "14") 

graph save annual_norw_with_natives.gph, replace
graph export annual_norw_with_natives.pdf, replace


clear 
gr combine  "annual_wages_with_natives.gph" "cumulative_wages_with_natives.gph"
gr save fig2_grc.gph, replace
gr export fig2_grc.pdf, replace

 
grc1leg  "annual_wages_with_natives.gph" "cumulative_wages_with_natives.gph", legendfrom( "annual_wages_with_natives.gph")  
gr_edit legend.plotregion1.label[1].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.key[1].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.label[3].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.key[3].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.label[2].style.editstyle size(small) editcopy
gr_edit legend.plotregion1.label[2].text = {}
gr_edit legend.plotregion1.label[2].text.Arrpush Enrollees
gr_edit legend.plotregion1.label[4].text = {}
gr_edit legend.plotregion1.label[4].text.Arrpush Non-enrollees
gr_edit legend.plotregion1.label[4].style.editstyle size(small) editcopy
gr_edit legend.plotregion1.label[5].style.editstyle size(small) editcopy
gr_edit legend.plotregion1.label[5].text = {}
gr_edit legend.plotregion1.label[5].text.Arrpush Natives
gr_edit legend.plotregion1.key[2].DragBy 69.10919540229885 -51.29310344827583
gr_edit legend.plotregion1.label[2].DragBy 68.67816091954026 -51.43678160919537
gr_edit legend.plotregion1.key[4].DragBy 73.41954022988504 -51.29310344827583
gr_edit legend.plotregion1.label[4].DragBy 72.98850574712645 -51.43678160919537
gr_edit legend.plotregion1.key[5].DragBy 75.7183908045977 -51.29310344827583
gr_edit legend.plotregion1.label[5].DragBy 75.28735632183911 -51.43678160919537

graph save fig2.gph, replace

 
graph export fig2.pdf, replace

grc1leg "cumulative_union_with_natives.gph" "annual_norw_with_natives.gph", position(1)  ring(0)
gr_edit legend.plotregion1.label[1].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.key[1].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.label[3].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.key[3].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.label[2].style.editstyle size(small) editcopy
gr_edit legend.plotregion1.label[2].text = {}
gr_edit legend.plotregion1.label[2].text.Arrpush Enrollees
gr_edit legend.plotregion1.label[4].style.editstyle size(small) editcopy
gr_edit legend.plotregion1.label[4].text = {}
gr_edit legend.plotregion1.label[4].text.Arrpush Non-enrollees
gr_edit legend.plotregion1.label[5].style.editstyle size(small) editcopy
gr_edit legend.plotregion1.label[5].text = {}
gr_edit legend.plotregion1.label[5].text.Arrpush Natives
gr_edit legend.plotregion1.key[2].DragBy 1.293103448275891 -117.816091954023
gr_edit legend.plotregion1.key[4].DragBy 6.178160919540214 -117.816091954023
gr_edit legend.plotregion1.key[5].DragBy 8.764367816091928 -117.816091954023
gr_edit legend.plotregion1.label[2].DragBy .8620689655172292 -117.816091954023
gr_edit legend.plotregion1.label[4].DragBy 5.316091954022975 -117.816091954023
gr_edit legend.plotregion1.label[5].DragBy 8.189655172413799 -117.816091954023
 
graph save fig3.gph, replace
graph export fig3.pdf, replace




clear

import delimited "estcumulative_emp_native.gph.csv"

keep  v___000016 v___000017 v___000012 v___000013 v___00000y v___00000z v___00000u v___00000v v___00000q v___00000r v___00000m v___00000n v___00000i v___00000j v___00000e v___00000f v___00000a v___00000b v___000006 v___000007 v___000002 v___000003
g yr = _n
replace yr = (yr-12)*(-1)
egen b = rowfirst(v___000017 v___000013 v___00000z v___00000v v___00000r v___00000n v___00000j v___00000f v___00000b v___000007 v___000003)
keep yr b

graph use "cum_emp_piv.gph"
addplot: connected b yr, ms(O) ysc(r(0(1)10)) ylabel(2 4 6 8 10) color(gs14) xlabel(0 " " 1 "4" 2 "5" 3 "6" 4 "7" 5 "8" 6 "9" 7 "10" 8 "11" 9 "12" 10 "13" 11 "14") 

graph save cumemp_with_natives.gph, replace
graph export cumemp_with_natives.pdf, replace



clear
import delimited "estcumulative_sosass_native.gph.csv"

keep if set>10

keep v___00002f v___00002b v___000027 v___000023 v___00001z v___00001v v___00001r v___00001n v___00001j v___00001f v___00001b 

g yr = _n
replace yr = (yr-12)*(-1)
egen b = rowfirst( v___00002f v___00002b v___000027 v___000023 v___00001z v___00001v v___00001r v___00001n v___00001j v___00001f v___00001b)
keep yr b

graph use "cum_sosass_piv.gph"
addplot: connected b yr, ms(O) ysc(r(0(1)25)) ylabel(5 10 15 20 25) color(gs14) xlabel(0 " " 1 "4" 2 "5" 3 "6" 4 "7" 5 "8" 6 "9" 7 "10" 8 "11" 9 "12" 10 "13" 11 "14")  

graph save cumsos_with_natives.gph, replace
graph export cumsos_with_natives.pdf, replace

grc1leg  "cumsos_with_natives.gph"  "cumemp_with_natives.gph",  legendfrom("cumsos_with_natives.gph")
gr_edit legend.plotregion1.label[1].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.key[1].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.label[3].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.key[3].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.label[2].style.editstyle size(small) editcopy
gr_edit legend.plotregion1.label[2].text = {}
gr_edit legend.plotregion1.label[2].text.Arrpush Enrollees
gr_edit legend.plotregion1.label[4].text = {}
gr_edit legend.plotregion1.label[4].text.Arrpush Non-enrollees
gr_edit legend.plotregion1.label[4].style.editstyle size(small) editcopy
gr_edit legend.plotregion1.label[5].style.editstyle size(small) editcopy
gr_edit legend.plotregion1.label[5].text = {}
gr_edit legend.plotregion1.label[5].text.Arrpush Natives
gr_edit legend.plotregion1.key[2].DragBy 69.56521739130437 -51.99275362318838
gr_edit legend.plotregion1.label[2].DragBy 69.02173913043478 -51.99275362318838
gr_edit legend.plotregion1.key[4].DragBy  72.82608695652172  -51.99275362318838
gr_edit legend.plotregion1.label[4].DragBy 72.10144927536233 -51.99275362318838
gr_edit legend.plotregion1.key[5].DragBy 74.27536231884058  -51.99275362318838
gr_edit legend.plotregion1.label[5].DragBy 73.91304347826087 -51.99275362318838

graph save fig_empsos.gph, replace
graph export fi_empsos.pdf, replace


grc1leg   "cumemp_with_natives.gph"  "cumsos_with_natives.gph",  legendfrom("cumsos_with_natives.gph")
gr_edit legend.plotregion1.label[1].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.key[1].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.label[3].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.key[3].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.label[2].style.editstyle size(small) editcopy
gr_edit legend.plotregion1.label[2].text = {}
gr_edit legend.plotregion1.label[2].text.Arrpush Enrollees
gr_edit legend.plotregion1.label[4].text = {}
gr_edit legend.plotregion1.label[4].text.Arrpush Non-enrollees
gr_edit legend.plotregion1.label[4].style.editstyle size(small) editcopy
gr_edit legend.plotregion1.label[5].style.editstyle size(small) editcopy
gr_edit legend.plotregion1.label[5].text = {}
gr_edit legend.plotregion1.label[5].text.Arrpush Natives
gr_edit legend.plotregion1.key[2].DragBy 69.56521739130437 -51.99275362318838
gr_edit legend.plotregion1.label[2].DragBy 69.02173913043478 -51.99275362318838
gr_edit legend.plotregion1.key[4].DragBy  72.82608695652172  -51.99275362318838
gr_edit legend.plotregion1.label[4].DragBy 72.10144927536233 -51.99275362318838
gr_edit legend.plotregion1.key[5].DragBy 74.27536231884058  -51.99275362318838
gr_edit legend.plotregion1.label[5].DragBy 73.91304347826087 -51.99275362318838

graph save fig_empsos2.gph, replace
graph export fi_empsos2.pdf, replace


grc1leg  "annual_wages_with_natives.gph" "cumulative_wages_with_natives.gph" "cumemp_with_natives.gph" , c(3) legendfrom("cumemp_with_natives.gph")
gr_edit legend.plotregion1.label[1].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.key[1].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.label[3].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.key[3].draw_view.setstyle, style(no)
gr_edit legend.plotregion1.label[2].style.editstyle size(small) editcopy
gr_edit legend.plotregion1.label[2].text = {}
gr_edit legend.plotregion1.label[2].text.Arrpush Enrollees
gr_edit legend.plotregion1.label[4].text = {}
gr_edit legend.plotregion1.label[4].text.Arrpush Non-enrollees
gr_edit legend.plotregion1.label[4].style.editstyle size(small) editcopy
gr_edit legend.plotregion1.label[5].style.editstyle size(small) editcopy
gr_edit legend.plotregion1.label[5].text = {}
gr_edit legend.plotregion1.label[5].text.Arrpush Natives
gr_edit legend.plotregion1.key[2].DragBy 70.65217391304344 -53.62318840579707
gr_edit legend.plotregion1.label[2].DragBy 70.4710144927536 -53.62318840579707
gr_edit legend.plotregion1.key[4].DragBy  75.54347826086958  -53.62318840579707
gr_edit legend.plotregion1.label[4].DragBy 75.36231884057969 -53.62318840579707
gr_edit legend.plotregion1.key[5].DragBy 78.98550724637677  -53.62318840579707
gr_edit legend.plotregion1.label[5].DragBy 78.44202898550724 -53.62318840579707

graph save fig_alt.gph, replace
graph export fi_alt.pdf, replace



